using MediPlatform.Write.Api.Models;
using MediPlatform.Infeartructure;
using MediPlatform.Domain.Order;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;

namespace MediPlatform.Write.Api.Services
{
    /// <summary>
    /// 退款申请写入服务
    /// </summary>
    public class RefundApplicationWriteService
    {
        private readonly IBaseRepository<RefundApplication> _refundApplicationRepository;
        private readonly IBaseRepository<RefundLog> _refundLogRepository;
        private readonly ILogger<RefundApplicationWriteService> _logger;

        public RefundApplicationWriteService(
            IBaseRepository<RefundApplication> refundApplicationRepository,
            IBaseRepository<RefundLog> refundLogRepository,
            ILogger<RefundApplicationWriteService> logger)
        {
            _refundApplicationRepository = refundApplicationRepository;
            _refundLogRepository = refundLogRepository;
            _logger = logger;
        }

        /// <summary>
        /// 确认退款申请
        /// </summary>
        /// <param name="request">确认退款请求</param>
        /// <returns>处理结果</returns>
        public async Task<bool> ConfirmRefundAsync(ConfirmRefundRequest request)
        {
            try
            {
                // 获取退款申请
                var refundApplication = await _refundApplicationRepository.GetValue(request.Id);
                if (refundApplication == null)
                {
                    _logger.LogWarning("退款申请不存在，ID：{Id}", request.Id);
                    return false;
                }

                // 检查状态是否为待处理
                if (refundApplication.Status != 0) // 0-待处理
                {
                    _logger.LogWarning("退款申请状态不正确，ID：{Id}，当前状态：{Status}", request.Id, refundApplication.Status);
                    return false;
                }

                // 更新退款申请状态
                refundApplication.Status = 1; // 1-已处理
                refundApplication.ProcessTime = DateTime.Now;
                refundApplication.ProcessorId = request.OperatorId;
                refundApplication.Remark = request.Remark;

                // 保存退款申请
                await _refundApplicationRepository.Update(refundApplication);

                // 创建退款日志
                var refundLog = new RefundLog
                {
                    ApplicationId = refundApplication.Id,
                    Status = 1, // 1-已处理
                    OperatorId = request.OperatorId,
                    OperateTime = DateTime.Now,
                    Remark = request.Remark
                };

                await _refundLogRepository.AddAsync(refundLog);

                _logger.LogInformation("退款申请确认成功，ID：{Id}，操作员：{OperatorId}", request.Id, request.OperatorId);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "确认退款申请失败，ID：{Id}", request.Id);
                return false;
            }
        }

        /// <summary>
        /// 拒绝退款申请
        /// </summary>
        /// <param name="request">拒绝退款请求</param>
        /// <returns>处理结果</returns>
        public async Task<bool> RejectRefundAsync(RejectRefundRequest request)
        {
            try
            {
                // 获取退款申请
                var refundApplication = await _refundApplicationRepository.GetValue(request.Id);
                if (refundApplication == null)
                {
                    _logger.LogWarning("退款申请不存在，ID：{Id}", request.Id);
                    return false;
                }

                // 检查状态是否为待处理
                if (refundApplication.Status != 0) // 0-待处理
                {
                    _logger.LogWarning("退款申请状态不正确，ID：{Id}，当前状态：{Status}", request.Id, refundApplication.Status);
                    return false;
                }

                // 更新退款申请状态
                refundApplication.Status = 2; // 2-已拒绝
                refundApplication.ProcessTime = DateTime.Now;
                refundApplication.ProcessorId = request.OperatorId;
                refundApplication.Remark = request.Remark;

                // 保存退款申请
                await _refundApplicationRepository.Update(refundApplication);

                // 创建退款日志
                var refundLog = new RefundLog
                {
                    ApplicationId = refundApplication.Id,
                    Status = 2, // 2-已拒绝
                    OperatorId = request.OperatorId,
                    OperateTime = DateTime.Now,
                    Remark = request.Remark
                };

                await _refundLogRepository.AddAsync(refundLog);

                _logger.LogInformation("退款申请拒绝成功，ID：{Id}，操作员：{OperatorId}", request.Id, request.OperatorId);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "拒绝退款申请失败，ID：{Id}", request.Id);
                return false;
            }
        }
    }
} 